package com.example.musicapp.base;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public abstract class BaseActivity<P extends IBasePresenter> extends AppCompatActivity implements IBaseView {

    private P mPresenter;

    /**
     * 创建相应的presenter对象
     *
     * @return 返回相应的presenter对象
     */
    public abstract P onBindPresenter();


    /*** 获取 Presenter 对象，在需要获取时才创建`Presenter`，起到懒加载作用 */
    public P getPresenter() {
        if (mPresenter == null) {
            mPresenter = onBindPresenter();
        }
        return mPresenter;
    }

    @Override
    public Activity getSelfActivity() {
        return this;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( getContentViewId());
        mPresenter = onBindPresenter();
        initView();
        initListener();
        initData();

    }

    protected abstract void initData();

    protected abstract void initView();

    /**
     * 初始化监听者的方法
     */
    public abstract void initListener();

    /**
     * 找viewId的方法
     * @return 返回找到的id
     */

    protected abstract int getContentViewId();

    @Override
    public void showLoading() { }

    @Override
    public void hideLoading() {}

    @Override
    public void showToast(String msg) {
    }


    protected void onDestroy() {
        hideLoading();
        super.onDestroy(); /* 在生命周期结束时，将 presenter 与 view 之间的联系断开，防止出现内存泄露 */
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}

